<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Gets the Unix timestamp</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="datetime.getoffset.html">« DateTimeInterface::getOffset</a></li>
      <li style="float: right;"><a href="datetime.gettimezone.html">DateTimeInterface::getTimezone »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.datetimeinterface.html">DateTimeInterface</a></li>
    <li>Gets the Unix timestamp</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="datetime.gettimestamp" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTimeInterface::getTimestamp</h1>
  <h1 class="refname">DateTimeImmutable::getTimestamp</h1>
  <h1 class="refname">DateTime::getTimestamp</h1>
  <h1 class="refname">date_timestamp_get</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateTimeInterface::getTimestamp</span> -- <span class="refname">DateTimeImmutable::getTimestamp</span> -- <span class="refname">DateTime::getTimestamp</span> -- <span class="refname">date_timestamp_get</span> &mdash; <span class="dc-title">Gets the Unix timestamp</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetime.gettimestamp-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateTimeInterface::getTimestamp</strong></span>(): <span class="type">int</span></div>

  <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><strong>DateTimeImmutable::getTimestamp</strong></span>(): <span class="type">int</span></div>

  <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><strong>DateTime::getTimestamp</strong></span>(): <span class="type">int</span></div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><a href="function.date-timestamp-get.html" class="methodname">date_timestamp_get</a></span>(<span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$object</code></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   Gets the Unix timestamp.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-datetime.gettimestamp-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetime.gettimestamp-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the Unix timestamp representing the date.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-datetime.gettimestamp-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   If the timestamp cannot be represented as <span class="type">int</span>, a <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>
   is thrown. Prior to PHP 8.0.0, <strong><code>false</code></strong> was returned in this case.
   Still, the timestamp can be retrieved as <span class="type">string</span> by using
   <span class="methodname"><a href="datetime.format.html" class="methodname">DateTimeInterface::format()</a></span> with the <code class="literal">U</code> format.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-datetime.gettimestamp-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       These functions no longer return <strong><code>false</code></strong> on failure.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-datetime.gettimestamp-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-2039">
   <p><strong>示例 #1 <span class="function"><strong>DateTime::getTimestamp()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTimestamp</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>过程化风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_create</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">date_timestamp_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
1272509157
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-datetime.gettimestamp-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="datetime.settimestamp.html" class="function" rel="rdfs-seeAlso">DateTime::setTimestamp()</a> - 以 Unix 时间戳的方式设置日期和时间</span></li>
   <li class="member"><span class="function"><a href="datetime.format.html" class="function" rel="rdfs-seeAlso">DateTime::format()</a> - 按照指定格式返回格式化后的日期</span></li>
  </ul>
 </div>


</div></div></div></body></html>